<%@ page language="java" import="javax.sql.*,java.sql.*,javax.naming.*,java.io.*,java.net.*,java.util.*" errorPage="" %>
<%@ page import="java.io.*, java.util.zip.*, java.util.*" %>
<%@ page import="javax.mail.*"%>
<%@ page import="javax.mail.internet.*"%>
<%@ page import="javax.activation.*"%>
<%@ page contentType="text/html; charset=UTF-8" %>

<%!

public class SendMail{

        public  void SendMailMain( String fromAddr, String fromAddrName, String toAddr, String toAddrName, String subject, String msgBody){


                Properties prop = new Properties();
                prop.put("mail.smtp.host", "10.160.1.1");  //設定SMTP Server

                Session session = Session.getDefaultInstance(prop, null); //與SMTP Server取得連線

                try{

                        MimeMessage msg = new MimeMessage(session);

                        
                        //主旨
                        msg.setSubject(new String( subject.getBytes("UTF-8"), "UTF-8" ),"UTF-8");

                        //寄件人
                        //InternetAddress from = new InternetAddress("MBMS@cht.com.tw");
                        InternetAddress from = new InternetAddress(fromAddr);
                        from.setPersonal(fromAddrName, "UTF-8");  //設定寄件人姓名
                        msg.setFrom(from);


                        //收件人
//                       Address address = new InternetAddress(toAddr);
//                       msg.setRecipient(Message.RecipientType.TO, address);
                         InternetAddress to = new InternetAddress(toAddr);
                         to.setPersonal(toAddrName, "UTF-8");
                         msg.setRecipient(Message.RecipientType.TO, to);


                        //Address address = new InternetAddress("dolaamn@cht.com.tw");             //收件人
                        //msg.setRecipient(Message.RecipientType.TO, address);

                        //InternetAddress ccAddress = new InternetAddress("dolaamn@cht.com.tw");   //副本人
                        //msg.addRecipient(Message.RecipientType.CC, ccAddress);


                        Multipart mp = new MimeMultipart();

                        //設定本文區的內容
                        MimeBodyPart mbp1 = new MimeBodyPart();
                        mbp1.setContent(msgBody, "text/plain;charset=UTF-8");
                        mp.addBodyPart(mbp1);


                        msg.setContent(mp);

                        Transport.send(msg);

                } catch (MessagingException me) {
                     me.printStackTrace();
                } catch (Exception e) {
                     e.toString();
                }
        }

}

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>

<%


  try{

        String fromAddr = request.getParameter("fromaddr");
        String fromAddrName = request.getParameter("fromaddrname");
        String toAddr = request.getParameter("toaddr");
        String toAddrName = request.getParameter("toaddrname");
        String subject = request.getParameter("subject");
        String msgBody = request.getParameter("msgbody");

        String clientIP = request.getRemoteAddr();

        msgBody = msgBody + "\n IP: " + clientIP;   //執行者IP位址

        SendMail sendMail = new SendMail();
        sendMail.SendMailMain(fromAddr, fromAddrName, toAddr, toAddrName, subject, msgBody);


}catch(Exception e){
    out.println(e.toString());
}finally{

}

%>

</body>
</html>